﻿namespace com.crownpack.probe
{
    using System;

    using com.crownpack.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;

    class Program
    {
        #region Methods

        static void Main( string[ ] args )
        {
            Program o = new Program( );
            o.getStructure( "http://win-pv542842fga:8888/sites/cp" );
            //o.getInitiatives( "http://win-pv542842fga:8888/sites/cp/i2e", "Initiatives" );
            //o.getInitiatives( "http://cpserver30:40020/I2E", "InitiativesL" );
        }

        void getInitiatives( string url, string list )
        {
            try
            {
                Console.WriteLine( "Opening site " + url );
                using ( SPSite spSite = new SPSite( url ) )
                {
                    Console.WriteLine( "Opening web" );
                    using ( SPWeb spWeb = spSite.OpenWeb( ) )
                    {
                        Console.WriteLine( "Finding list " + list );
                        SPList spList = spWeb.Lists.GetListByRootFolder( list );
                        if ( spList == null )
                        {
                            Console.WriteLine( "List not found" );
                        }
                        else
                        {
                            Console.WriteLine( spList.DefaultViewUrl );
                            Console.WriteLine( "Number of items: " + spList.ItemCount );
                        }
                    }
                }
            }
            catch ( Exception e )
            {
                Console.WriteLine( e.Message );
                if ( e.InnerException != null )
                    Console.WriteLine( e.InnerException.Message );
            }
            Console.ReadLine( );
        }

        void getStructure( string url )
        {
            try
            {
                using ( SPSite spSite = new SPSite( url ) )
                {
                    using ( SPWeb spWeb = spSite.OpenWeb( ) )
                    {
                        Console.WriteLine( "Top url " + spWeb.Url );
                        foreach ( SPWeb w in spWeb.Webs )
                        {
                            getSubStructure( w.Url );
                        }
                    }
                }
            }
            catch ( Exception e )
            {
                Console.WriteLine( e.Message );
                if ( e.InnerException != null )
                    Console.WriteLine( e.InnerException.Message );
            }
            Console.ReadLine( );
        }

        void getSubStructure( string url )
        {
            try
            {
                using ( SPSite spSite = new SPSite( url ) )
                {
                    using ( SPWeb spWeb = spSite.OpenWeb( ) )
                    {
                        Console.WriteLine( "Url " + spWeb.Url );
                        foreach ( SPWeb w in spWeb.Webs )
                        {
                            getSubStructure( w.Url );
                        }
                    }
                }
            }
            catch ( Exception e )
            {
                Console.WriteLine( e.Message );
                if ( e.InnerException != null )
                    Console.WriteLine( e.InnerException.Message );
            }
        }

        #endregion Methods
    }
}